#include <REGX52.H>

sbit IIC_SCL = P0^7;
sbit IIC_SDA = P0^6;

void IIC_Start()
{
    IIC_SDA = 1;
    IIC_SCL = 1;
    IIC_SDA = 0;
    IIC_SCL = 0;
}

void IIC_Stop()
{
    IIC_SDA = 0;
    IIC_SCL = 1;
    IIC_SDA = 1;
}

void IIC_SendByte(unsigned char Byte)
{
    unsigned char i;
    for(i=0;i<8;i++)
    {
        IIC_SDA=Byte&(0x80>>i);
        IIC_SCL=1;
        IIC_SCL=0;
    }
}

unsigned char IIC_ReceiveByte()
{
    unsigned char i,Byte=0x00;
    IIC_SDA=1;
    for(i=0;i<8;i++)
    {
        IIC_SCL=1;
        if(IIC_SDA)
        {
            Byte|=(0x80>>i);
        }
        IIC_SCL=0;
    }
    return Byte;
}

void IIC_SendAck(bit AckBit)
{
    IIC_SDA=AckBit;
    IIC_SCL=1;
    IIC_SCL=0;
}

bit IIC_ReceiveAck()
{
    bit AckBit;
    IIC_SDA=1;
    IIC_SCL=1;
    AckBit=IIC_SDA;
    IIC_SCL=0;
    return AckBit;
}